<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Utils
{

    /**
     * Convert date to thai & enlisg
     * @param Date $date
     * @param String $type
     * @param String $separator
     * @return String date format 
     */
    public static function dateformat($date, $type='en-th', $mtype='mini', $separator='-')
    {

        $full_month = array('01' => 'มกราคม', '02' => 'กุมภาพันธ์', '03' => 'มีนาคม', '04' => 'เมษายน',
            '05' => 'พฤษภาคม', '06' => 'มิถุนายน', '07' => 'กรกฎาคม', '08' => 'สิงหาคม',
            '09' => 'กันยายน', '10' => 'ตุลาคม', '11' => 'พฤศจิกายน', '12' => 'ธันวาคม'
        );
        $quick_month = array('01' => 'ม.ค.', '02' => 'ก.พ.', '03' => 'มี.ค.', '04' => 'เม.ย.',
            '05' => 'พ.ค.', '06' => 'มิ.ย.', '07' => 'ก.ค.', '08' => 'ส.ค.',
            '09' => 'ก.ย.', '10' => 'ต.ค.', '11' => 'พ.ย.', '12' => 'ธ.ค.'
        );

        $m = date('m', strtotime($date));

        if ($mtype == 'full')
            $month = " " . $full_month[$m] . " ";
        else if ($mtype == 'mini')
            $month = " " . $quick_month[$m] . " ";
        else
            $month = date('-m-', strtotime($date));

        if ($type == 'th-en')
        {
            return (date('Y', strtotime($date)) - 543) . $separator . date('m' . $separator . 'd', strtotime($date));
        }
        else if ($type == 'en-th')
        {

            return date('d', strtotime($date)) . $month . (date('Y', strtotime($date)) + 543);
        }

        else
            return NULL;
    }

    /**
     * Display data from array with tag pre
     * @param Array $data 
     */
    public static function dump($data)
    {
        echo '<pre>';
        var_dump($data);
        echo '</pre>';
    }

    public static function getIp()
    {
        if (getenv('HTTP_X_FORWARDED_FOR') == "" || getenv('HTTP_X_FORWARDED_FOR') == "unknown")
        {
            return getenv('REMOTE_ADDR');
        }
        else
        {
            return getenv('HTTP_X_FORWARDED_FOR');
        }
    }

    public static function getClientBrowser()
    {
        $u_agent = $_SERVER['HTTP_USER_AGENT'];
        $bname = 'Unknown';
        $platform = 'Unknown';
        $version = "";

        //First get the platform?
        if (preg_match('/android/i', $u_agent))
        {
            $platform = 'Android';
        }
        elseif (preg_match('/linux/i', $u_agent))
        {
            $platform = 'Linux';
        }
        elseif (preg_match('/symbianos|symbos/i', $u_agent))
        {
            $platform = 'Symbian';
        }
        elseif (preg_match('/iphone/i', $u_agent))
        {
            $platform = 'iPhone';
        }
        elseif (preg_match('/macintosh|mac os x/i', $u_agent))
        {
            $platform = 'Mac';
        }
        elseif (preg_match('/windows|win32/i', $u_agent))
        {
            $platform = 'Windows';
        }

//Opera/9.80 (S60; SymbOS; Opera Mobi/498; U; th) Presto/2.4.18 Version/10.00
//Nokia3250/2.0 (03.24) SymbianOS/9.1 Series60/3.0 Profile/MIDP-2.0 Configuration/CLDC-1.1
////SonyEricssonU10i/R7BA Browser/NetFront/3.5 Profile/MIDP-2.1 Configuration/CLDC-1.1 JavaPlatform/JP-8.5.2
//SAMSUNG-GT-B3410W/1.0 SHP/VPP/R5 Dolfin/1.5 Nextreaming SMM-MMS/1.2.0 profile/MIDP-2.1 configuration/CLDC-1.1 OPN-N
////Mozilla/5.0 (Linux; U; Android 2.3.3; th-th; GT-P1000 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
//Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_5 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8L1 Safari/6533.18.5
        // Next get the name of the useragent yes seperately and for good reason
        if (preg_match('/MSIE/i', $u_agent) && !preg_match('/Opera/i', $u_agent))
        {
            $bname = 'Internet Explorer';
            $ub = "MSIE";
        }
        elseif (preg_match('/Firefox/i', $u_agent))
        {
            $bname = 'Mozilla Firefox';
            $ub = "Firefox";
        }
        elseif (preg_match('/Chrome/i', $u_agent))
        {
            $bname = 'Google Chrome';
            $ub = "Chrome";
        }
        elseif (preg_match('/Safari/i', $u_agent))
        {
            $bname = 'Apple Safari';
            $ub = "Safari";
        }
        elseif (preg_match('/Opera/i', $u_agent))
        {
            $bname = 'Opera';
            $ub = "Opera";
        }
        elseif (preg_match('/Netscape/i', $u_agent))
        {
            $bname = 'Netscape';
            $ub = "Netscape";
        }

        // finally get the correct version number
        $known = array('Version', $ub, 'other');
        $pattern = '#(?<browser>' . join('|', $known) .
                ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
        if (!preg_match_all($pattern, $u_agent, $matches))
        {
            // we have no matching number just continue
        }

        // see how many we have
        $i = count($matches['browser']);
        if ($i != 1)
        {
            //we will have two since we are not using 'other' argument yet
            //see if version is before or after the name
            if (strripos($u_agent, "Version") < strripos($u_agent, $ub))
            {
                $version = $matches['version'][0];
            }
            else
            {
                $version = $matches['version'][1];
            }
        }
        else
        {
            $version = $matches['version'][0];
        }

        // check if we have a number
        if ($version == null || $version == "")
        {
            $version = "?";
        }

        return array(
            'userAgent' => $u_agent,
            'name' => $bname,
            'version' => $version,
            'platform' => $platform,
            'pattern' => $pattern
        );
    }

    public static function arrayToObject($array)
    {
        if (!is_array($array))
        {
            return $array;
        }

        $object = new stdClass();
        if (is_array($array) && count($array) > 0)
        {
            foreach ($array as $name => $value)
            {
                $name = strtolower(trim($name));
                if (!empty($name))
                {
                    $object->$name = self::arrayToObject($value);
                }
            }
            return $object;
        }
        else
        {
            return FALSE;
        }
    }

}

?>
